package cn.commander.utils;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @Description: 重新spring读取配置文件
 * @Created by:
 * @Date: 2016/7/7 14:28
 * @Version: 1.0
 */
public class CustomizedPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {

    private static Map<String,String> ctxPropertiesMap;

    public CustomizedPropertyPlaceholderConfigurer() {
        ctxPropertiesMap = new HashMap<String,String>();
    }

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
                                     Properties props) throws BeansException {
        super.processProperties(beanFactoryToProcess, props);

        for (Object key : props.keySet()) {
            String keyStr = key.toString();
            String value = props.getProperty(keyStr);
            CustomizedPropertyPlaceholderConfigurer.ctxPropertiesMap.put(keyStr, value);
        }
    }


    public static Object getContextProperty(String name) {
        return CustomizedPropertyPlaceholderConfigurer.ctxPropertiesMap.get(name);
    }
}